# @Author   : 懒羊羊
# @File     : three.py
# @Time     : 2024/12/3 19:55
import time

from selenium.webdriver.chrome import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait


# wd是webdriver对象，10是最长等待时间，0.5是每0.5秒去查询对应的元素。
def getElement(d, Bye, Byv):
    wdt = WebDriverWait(d, 10, 0.5)
    try:
        ele = wdt.until(lambda d: d.find_element(Bye, Byv))
    except Exception as e:
        print(e)
    else:
        return ele


# wd =webdriver.WebDriver()

#
# wd.maximize_window()
# # 隐式等待，全局生效
# wd.implicitly_wait(10)

# 显示等待，只针对指定元素进行等待
# wd是webdriver对象，10是最长等待时间，0.5是每0.5秒去查询对应的元素。
# wdt = WebDriverWait(wd, 10, 0.5)
# until后面跟的等待具体条件
# wdt.until()

# 申请一个无头浏览器
# 第一步需要设置一下无头选项
# 设置之前需要一个选项的实例
# options = webdriver.Options()
options = Options()

options.add_argument("--headless")

wd = webdriver.WebDriver(options=options)


wd.get("https://email.163.com/")
# time.sleep(3)

# iframe = wd.find_element(By.XPATH, "//div[@class='urs-area']//iframe")
iframe = getElement(wd, By.XPATH, "//div[@class='urs-area']//iframe")
wd.switch_to.frame(iframe)

# wd.find_element(By.NAME, "email").send_keys("trl_1009")
# wd.find_element(By.XPATH, "//input[contains(@id, 'auto-id')]").send_keys("trl_1009")
# wd.find_element(By.XPATH, "//input[starts-with(@id, 'auto-id')]").send_keys("trl_1009")
getElement(wd, By.XPATH, "//input[starts-with(@id, 'auto-id')]").send_keys("trl_1009")

# wd.find_element(By.NAME, "password").send_keys("Qq7517517770.")
getElement(wd, By.NAME, "password").send_keys("Qq7517517770.")
time.sleep(1)

# wd.find_element(By.ID, "dologin").click()
getElement(wd, By.ID, "dologin").click()

time.sleep(3)

wd.quit()
